package com.bamboo.tool.template.yapi.view.actions;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.bamboo.tool.config.BambooToolComponent;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.entity.NoteData;
import com.bamboo.tool.entity.QueryClassParameter;
import com.bamboo.tool.template.yapi.entity.YapiSetting;
import com.bamboo.tool.template.yapi.view.dialog.UploadYapiDialogWrapper;
import com.bamboo.tool.view.component.tree.*;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import icons.PluginIcons;
import org.jetbrains.annotations.NotNull;

import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class UploadYapiAction extends DumbAwareAction {
    private ApiTree apiTree;
    private Project myProject;



    public UploadYapiAction(ApiTree apiTree, Project myProject) {
        super("上传Yapi", "", PluginIcons.TORNA_LOGO);
        this.apiTree = apiTree;
        this.myProject = myProject;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
        Project project = anActionEvent.getProject();

        TreePath[] selectionPaths = apiTree.getSelectionPaths();
        QueryClassParameter queryClassParam = new QueryClassParameter();
        for (TreePath selectionPath : selectionPaths) {
            Object lastPathComponent = selectionPath.getLastPathComponent();

            if (lastPathComponent instanceof ClassNode) {
                ClassNode classNode = (ClassNode) lastPathComponent;
                NoteData source = classNode.getSource();
                queryClassParam.getClassIds().add(source.getClassId());
            }
            if (lastPathComponent instanceof ModuleNode) {
                ModuleNode moduleNode = (ModuleNode) lastPathComponent;
                Integer projectId = moduleNode.getProjectId();
                Map<Integer, List<String>> projects = queryClassParam.getProjects();
                List<String> models = projects.get(projectId);
                if (CollectionUtil.isEmpty(models)) {
                    models = new ArrayList<>();
                    projects.put(projectId, models);
                }
                models.add(moduleNode.getSource());
            }
            if (lastPathComponent instanceof ProjectNode) {


            }
            if (lastPathComponent instanceof MethodNode) {
                MethodNode methodNode = (MethodNode) lastPathComponent;
                queryClassParam.getMethodIds().add(methodNode.getSource().getMethodId());
            }
            if (lastPathComponent instanceof RootNode) {
                List<Integer> projectIds = BambooToolComponent.getStoreService().getAllProjectInfos().stream().map(e -> e.getId()).collect(Collectors.toList());
                projectIds.stream().forEach(e -> {
                    queryClassParam.getProjects().put(e, new ArrayList<>());
                });
            }
        }

        YapiSetting yapiSetting = RestfulCloudApplicationCache.getInstance().getState().getYapiSetting();
        if (StrUtil.isEmpty(yapiSetting.getToken()) || StrUtil.isEmpty(yapiSetting.getUserName()) || StrUtil.isEmpty(yapiSetting.getPassword())) {
            ShowSettingsUtil.getInstance().showSettingsDialog(myProject, "Yapi Setting");
        } else {
            new UploadYapiDialogWrapper(queryClassParam, project).showAndGet();
        }
    }
}
